﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Data;
using DataAccess;
using Utility;

namespace Website.UC
{
    public partial class UC_Login : System.Web.UI.UserControl
    {
        LoginControl loginControl = new LoginControl();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUser.Text.Length == 0 || txtPass.Text.Length == 0)
            {
                WebHelper.ShowMessage("username anf password can not null !!", Page);
            }
            else
            {
                try
                {

              

                    if (ddlRole.SelectedIndex == 0 ) // student
                    {
                        tbl_student student = loginControl.studentLogin(txtUser.Text, txtPass.Text);
                        if (!student.username.Equals("") || student.username.Length > 0)
                        {
                            Session["student"] = student.username;
                            Response.Redirect("Student_page.aspx");
                        }
                        else
                        {
                            WebHelper.ShowMessage("Login fail !!", Page);
                        }
                    }
                    else if (ddlRole.SelectedIndex == 1) // teacher
                    {
                        tbl_teacher teacher = loginControl.teacherlogin(txtUser.Text, txtPass.Text);
                        if (!teacher.username.Equals("") || teacher.username.Length > 0)
                        {
                            Session["teacher"] = teacher.username;
                            Response.Redirect("Teacher_Page.aspx");
                        }
                        else
                        {
                            WebHelper.ShowMessage("Login fail !!", Page);
                        }
                    }
                    else if (ddlRole.SelectedIndex == 2 ) // admin
                    {
                        tbl_admin admin = loginControl.adminlogin(txtUser.Text, txtPass.Text);
                        if (!admin.username.Equals("") || admin.username.Length > 0)
                        {
                            Session["admin"] = admin.username;
                            Response.Redirect("Admin_page.aspx");
                        }
                        else
                        {
                            WebHelper.ShowMessage("Login fail !!", Page);
                        }
                    }
                }
                catch(Exception ex){

     
                    WebHelper.ShowMessage("Login fail !!", Page);
                }
  
            }
        }
    }
}